package com.bussiness.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bussiness.auto.JSONAuthentication;
import com.bussiness.entity.BaApartment;
import com.bussiness.entity.BuUser;
import com.bussiness.entity.UserMeans;
import com.bussiness.mapper.BaApartmentMapper;
import com.bussiness.mapper.BuUserMapper;
import com.bussiness.mapper.UserMeansMapper;
import com.bussiness.response.Result;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bussiness
 * @since 2022-03-29
 */
@RestController
@RequestMapping("/ba-apartment")
public class BaApartmentController extends JSONAuthentication {
    @Autowired
    BaApartmentMapper baApartmentMapper;
    @ApiModelProperty
    BuUserMapper buUserMapper;
    @Autowired
    UserMeansMapper userMeansMapper;
    @ApiOperation(value = "添加部门")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestBody BaApartment baApartment) throws  Exception{
        if(baApartmentMapper.insert(baApartment)>0){
            Result ok = Result.ok().message("添加成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除部门")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=baApartmentMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改部门经理")
    @PostMapping("/upd")
    public void updMeans(HttpServletRequest request, HttpServletResponse response,  @RequestParam("userId") int userId,@RequestParam("id") int id ) throws Exception{
        BaApartment baApartment = baApartmentMapper.selectById(id);
        baApartment.setUserId(userId);
        if(baApartmentMapper.updateById(baApartment)>0){
            Result ok = Result.ok().message("修改成功");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有部门")
    @PostMapping("/findAllApart")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<BaApartment> shUserPage=new Page<>(current,size);
        Page<BaApartment> shUserPages=baApartmentMapper.selectPage(shUserPage,null);
        List<BaApartment> records = shUserPages.getRecords();
        int total=baApartmentMapper.selectCount(null);
        for (BaApartment a :records){
            QueryWrapper wrapper1=new QueryWrapper();
            wrapper1.eq("user_id",a.getUserId());
            if(userMeansMapper.selectOne(wrapper1)!=null){
                a.setUserMeans(userMeansMapper.selectOne(wrapper1));
            }
        }
        if(records.size()>0){
            Result ok = Result.ok().data("apart",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有部门按照部门名字")
    @PostMapping("/findAllApartByName")
    public void findAllApartByName(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("name") String name)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("name",name);
        Page<BaApartment> shUserPage=new Page<>(current,size);
        Page<BaApartment> shUserPages=baApartmentMapper.selectPage(shUserPage,wrapper);
        List<BaApartment> records = shUserPages.getRecords();
        int total=baApartmentMapper.selectCount(wrapper);
        for (BaApartment a :records){
            QueryWrapper wrapper1=new QueryWrapper();
            wrapper1.eq("user_id",a.getUserId());
            if(userMeansMapper.selectOne(wrapper1)!=null){
                a.setUserMeans(userMeansMapper.selectOne(wrapper1));
            }
        }
        if(records.size()>0){
            Result ok = Result.ok().data("apart",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

